﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.DataClientDBAgent;
using HF.Framework.Web;
using HF.Framework.SqlDataContract;
using HF.Business.OA;
using HF.User.Data;
using HF.WorkFlow.Const;

public partial class ModulePages_Address_NEWSAdd : System.Web.UI.Page
{
    string taskId = "";
    string state = "";
    string UserId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        UserId = User.Identity.Name;
        if (Request["State"] != null)
        {
            state = Request["State"].ToString();
            if (state == WorkFlowConst.STATE_ADD)
                tbxSubmitMan.Text = UserData.GetInstance().GetUserNameById(UserId);
        }
        else
            state = WorkFlowConst.STATE_VIEW;
        foreach (Control cl in this.Controls)
        {
            setReadOnly(cl);
        }

        if (Request["TaskId"] != null)
        {
            taskId = Request["TaskId"].ToString();

            if (!IsPostBack)
            {
                OA_NEWS oa = new OA_NEWS();
                oa.GetOA_NEWSInfo(taskId);

                this.txtsubject.Text = oa.Title;
                this.txtcomment.Text = oa.Content;
                this.txtDate.Text = oa.SubmitDate;
                this.tbxSubmitMan.Text = oa.SubmitMan;
                this.txtTaskId.Text = oa.TaskId;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (state == WorkFlowConst.STATE_ADD)
        {

            OA_NEWS no = new OA_NEWS();
            no.SubmitDate = this.txtDate.Text;
            no.Title = this.txtsubject.Text;
            no.Content = this.txtcomment.Text;
            no.TaskId = Guid.NewGuid().ToString();
            no.SubmitMan = tbxSubmitMan.Text;
            no.Insert();


        }
        else
        {
            OA_NEWS no = new OA_NEWS();
            no.SubmitDate = this.txtDate.Text;
            no.Title = this.txtsubject.Text;
            no.Content = this.txtcomment.Text;
            no.SubmitMan = tbxSubmitMan.Text;
            no.TaskId = this.txtTaskId.Text;
            no.Update();
        }
        WorkDialog.ShowMessage(this, "保存成功!");
        btnSave.Enabled = false;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close()</script>");
    }

    public void setReadOnly(Control crl)
    {
        if (state == WorkFlowConst.STATE_VIEW)
        {
            string ctrlTypeName = "";
            foreach (Control c in crl.Controls)
            {
                ctrlTypeName = c.GetType().Name;
                switch (ctrlTypeName)
                {
                    case "Button":
                        (c as Button).Enabled = false;
                        break;
                    case "DrapDownList":
                        (c as Button).Enabled = false;
                        break;
                    case "TextBox":
                        (c as TextBox).ReadOnly = true;
                        break;
                }
                if (c.HasControls())
                    setReadOnly(c);
            }
        }
    }
}
